<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
			<el-form-item label="主键" prop="id" v-if="false">
				<el-input v-model="dataForm.id" placeholder="主键"></el-input>
			</el-form-item>

			<el-form-item label="名称" prop="name">
				<el-input v-model="dataForm.name" placeholder="名称"></el-input>
			</el-form-item>
			<el-form-item label="培训封面" prop="imgUrl">
				<UploadImgView v-model="dataForm.imgUrl"></UploadImgView>
			</el-form-item>
			<el-form-item label="培训分类" prop="typeId">
				<el-tree-select
					v-model="dataForm.typeId"
					:data="dataPList"
					ref="treeModel"
					value-key="id"
					check-strictly
					:render-after-expand="false"
					:props="{ value: 'id', label: 'name', children: 'children' }"
					style="width: 100%"
				/>
			</el-form-item>
			<el-form-item label="培训简介" prop="description">
				<WangEditor v-model="dataForm.description" placeholder="请输入..."></WangEditor>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="dataForm.remark" placeholder="备注" type="textarea"></el-input>
			</el-form-item>
			<el-form-item label="计划开始时间" prop="startDate">
				<ElDatePicker v-model="dataForm.startDate" placeholder="计划开始时间" />
			</el-form-item>
			<el-form-item label="计划结束时间" prop="endDate">
				<ElDatePicker v-model="dataForm.endDate" placeholder="计划结束时间" />
			</el-form-item>
			<el-form-item label="预计培训人数" prop="population">
				<el-input v-model="dataForm.population" placeholder="预计培训人数" type="number">
					<template #append>人</template>
				</el-input>
			</el-form-item>
			<el-form-item label="可用范围" prop="isAll">
				<el-switch
					v-model="dataForm.isAll"
					:active-value="'1'"
					:inactive-value="'0'"
					active-text="全部"
					inline-prompt
					inactive-text="指定"
					placeholder=""
				/>
			</el-form-item>
			
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useTrainApi, useTrainSubmitApi } from '@/api/nb/train/train'
import UploadImgView from '@/views/nb_module/image/upload-img-view.vue'
import WangEditor from '@/components/wang-editor/index.vue'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()
const dataPList = ref([])

const dataForm = reactive({
	id: '',

	name: '',
	imgUrl: '',
	typeId: '',
	description: '',
	remark: '',
	startDate: '',
	endDate: '',
	population: '',
	isAll: '1'
})

const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	if (id) {
		getTrain(id)
	}
}

const getTrain = (id: number) => {
	useTrainApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		useTrainSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init,
	dataPList
})
</script>
